import router from "@ohos.router";

export class RouterUtils {
  /**
   * 安全获取路由参数
   */
  static getSafeParams(): Record<string, any> {
    return router.getParams() || {};
  }

  /**
   * 获取数字类型的参数
   */
  static getNumberParam(key: string, defaultValue: number = 0): number {
    const params = this.getSafeParams();
    const value = params[key];

    if (typeof value === 'number') {
      return value;
    }

    if (typeof value === 'string') {
      const num = parseInt(value);
      return isNaN(num) ? defaultValue : num;
    }

    return defaultValue;
  }

  /**
   * 获取字符串类型的参数
   */
  static getStringParam(key: string, defaultValue: string = ''): string {
    const params = this.getSafeParams();
    const value = params[key];

    if (typeof value === 'string') {
      return value;
    }

    if (typeof value === 'number') {
      return value.toString();
    }

    return defaultValue;
  }
}